import React from 'react'
import ReactDOM, { createRoot } from 'react-dom/client'
import App from './App.tsx'
import './index.css'
import {
  renderWithQiankun,
  qiankunWindow,
} from 'vite-plugin-qiankun/dist/helper'

function render(props: any) {
  const { container } = props
  ReactDOM.createRoot(
    container
      ? container.querySelector('#root')
      : document.getElementById('root')!
  ).render(
    <React.StrictMode>
      <App />
    </React.StrictMode>
  )
}

renderWithQiankun({
  mount(props) {
    console.log('mount')
    render(props)
  },
  bootstrap() {
    console.log('bootstrap')
  },
  update() {},
  unmount(props: any) {
    console.log('unmount')
    const { container } = props
    const root = createRoot(
      container
        ? container.querySelector('#root')
        : document.getElementById('root')!
    )
    root.unmount()
  },
})

if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  render({})
}
